function uuid(){
	return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,function (c) {
		var r =Math.random() * 16 | 0 ,
		 v = c == 'x' ? r: (r & 0x3|0x8);
		return v.toString(16);
	})
}


layui.use(['form'], function () {
    var form = layui.form,
        layer = layui.layer,
        $ = layui.$;
    
    var dealerSelect = document.getElementById("dealer_id");
    $.ajax({
        url:'/Dealer/list',
        type: "Post",
        data: '{"page":1,"limit":15}',
        datatype:"json",
        success:function(data){
            // console.log("Debug data：",data);
            let optEle1 = document.createElement("option");
            optEle1.setAttribute("value","default");
            optEle1.appendChild(document.createTextNode("===选择经销商==="));
            dealerSelect.appendChild(optEle1);

            for (var x=0 ; x<data.data.length;x++){
                let optEle1 = document.createElement("option");
                optEle1.setAttribute("value",data.data[x].dealer_uid);
                optEle1.appendChild(document.createTextNode(data.data[x].dealer));
                dealerSelect.appendChild(optEle1);

            }
            layui.form.render("select");
        }
    });
    
    //监听提交
    form.on('submit(saveBtn)', function (data) {
        var confirm = layer.confirm("确认添加？",  function () {
            layer.close(confirm);
            // console.log("Debug sub_data",data.field);
            // data.field.uid = uuid();
            $.ajax({
                url:"/Goods/add",
                type:"post",
                data:JSON.stringify([data.field]),
                datatype:"json",
                success: (res) => {
                    // console.log("Debug add uuid:",data.field);
                    var alert = layer.alert(JSON.stringify(res), {
                        title: '最终的提交信息'
                    }, function () {
                    
                        // 关闭弹出层
                        layer.close(alert)

                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    });
                }
            });
        });
        return false;
    });
});
